package xyz.xtt.pay.api.enums;

public enum PayOrderStatusEnum {
	UN_PAY(0, "未支付"), PAY_SUCCESS(1, "支付成功"), PAY_FAIL(2, "支付失败(终态)"), PAY_CANCEL(3, "支付取消(终态)"), REFUND_SUCCESS(4, "已退款成功(终态)"), PAYING(5, "支付中"),;

	private int code;
	private String desc;

	PayOrderStatusEnum(int code, String desc) {
		this.code = code;
		this.desc = desc;
	}

	public int getCode() {
		return code;
	}

	public static PayOrderStatusEnum getByCode(int code) {
		for (PayOrderStatusEnum value : PayOrderStatusEnum.values()) {
			if (value.code == code) {
				return value;
			}
		}
		return null;
	}

	/**
	 * 根据当前状态判断是否可以更新到目标状态
	 */
	public boolean canUpdateTo(PayOrderStatusEnum target) {
		if (this == target) {
			return false;
		}
		switch (this) {
        case UN_PAY:
            return target == PAY_SUCCESS || target == PAY_FAIL || target == PAY_CANCEL || target == PAYING;
        case PAYING:
            return target == PAY_SUCCESS || target == PAY_FAIL || target == PAY_CANCEL;
        case PAY_SUCCESS:
            return target == REFUND_SUCCESS;
        default:
            return false;
    }
	}
}
